/*
 * COPYRIGHT. ShenZhen JiMi Technology Co., Ltd. 2018.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen JiMi Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * 2018年5月18日    yaojianping         Create the class
 * http://www.jimilab.com/
*/

package com.jimi.openapi;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.jimi.openapi.util.SignUtils;

/**
 * @FileName OpenApiDemo.java
 * @Description: 
 *
 * @Date 2018年5月18日 下午5:53:13
 * @author yaojianping
 * @version 1.0
 */
public class OpenApiDemo {

	private static final String openapi_url = "http://open.aichezaixian.com/route/rest";
	
	// 申请来的appKey和appSecret
	private static final String app_key = "申请的key";
	private static final String app_secret = "申请的secret";
	
	public static void main(String[] args) {
		Map<String, String> headerMap = new HashMap<String, String>();
		headerMap.put("Content-Type", "application/x-www-form-urlencoded");
		
		Map<String, String> paramMap = new HashMap<String, String>();
		// 公共参数
		paramMap.put("app_key", app_key);
		paramMap.put("v", "1.0");
		paramMap.put("timestamp", getCurrentDate());
		paramMap.put("sign_method", "md5");
		paramMap.put("format", "json");
		paramMap.put("method", "jimi.oauth.token.get");
		
		// 私有参数
		paramMap.put("user_id", "您的账号");
		paramMap.put("user_pwd_md5", DigestUtils.md5Hex("您的密码"));
		paramMap.put("expires_in", "120");
		
		// 计算签名
		String sign = "";
		try {
			sign = SignUtils.signTopRequest(paramMap, app_secret, "md5");
			paramMap.put("sign", sign);
		} catch (IOException e) {
			System.err.println(e);
		}
		
		sendPost(headerMap, paramMap);
	}
	
	
	private static void sendPost(Map<String, String> headerMap, Map<String, String> paramMap) {
		try {
			HttpPost post = new HttpPost(openapi_url);
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			for (String key : paramMap.keySet()) {
				list.add(new BasicNameValuePair(key, paramMap.get(key)));
			}
			post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
			if (null != headerMap) {
				post.setHeaders(assemblyHeader(headerMap));
			}
			
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpResponse response = httpClient.execute(post);
			HttpEntity entity = response.getEntity();
			System.out.println(EntityUtils.toString(entity, "utf-8"));

		} catch (IOException e) {
			System.err.println(e);
		}
	}
	
	/**
	 * 组装头部信息
	 * 
	 * @param headers
	 * @return
	 */
	private static Header[] assemblyHeader(Map<String, String> headers) {
		Header[] allHeader = new BasicHeader[headers.size()];
		int i = 0;
		for (String str : headers.keySet()) {
			allHeader[i] = new BasicHeader(str, headers.get(str));
			i++;
		}
		return allHeader;
	}

	public static String getCurrentDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String result = formatter.format(new Date());
		return result;
	}
}
